Imports PTT.CarRes.Data

Public Class ClassQueryInvDriverCost
    Public Function Add(ByVal InvDriverCost As QueryInvDriverCost, ByVal StrConn As String) As Boolean
        Dim ClsInvDriverCost As New ClassInvDriverCost
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDriverCost.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvDriverCost
            .CostId = InvDriverCost.CostId
            .CostDay = InvDriverCost.CostDay
            .CostHoliday = InvDriverCost.CostHoliday
            .OtDay = InvDriverCost.OtDay
            .OtHoliday = InvDriverCost.OtHoliday
            .Costnight = InvDriverCost.Costnight
            ret = .USP_Save(Conn)
        End With
        ClsInvDriverCost.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Edit(ByVal InvDriverCost As QueryInvDriverCost, ByVal StrConn As String) As Boolean
        Dim ClsInvDriverCost As New ClassInvDriverCost
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDriverCost.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvDriverCost
            .CostId = InvDriverCost.CostId
            .Load(Conn)
            .CostDay = InvDriverCost.CostDay
            .CostHoliday = InvDriverCost.CostHoliday
            .OtDay = InvDriverCost.OtDay
            .OtHoliday = InvDriverCost.OtHoliday
            .Costnight = InvDriverCost.Costnight
            ret = .USP_Update(Conn)
        End With
        ClsInvDriverCost.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Delete(ByVal InvDriverCost As QueryInvDriverCost, ByVal StrConn As String) As Boolean
        Dim ClsInvDriverCost As New ClassInvDriverCost
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDriverCost.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvDriverCost
            .CostId = InvDriverCost.CostId
            .Load(Conn)
            ret = .USP_Delete(Conn)
        End With
        ClsInvDriverCost.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Search(Optional ByVal ParaCostId As String = "", Optional ByVal StrConn As String = "") As QueryInvDriverCosts
        Dim ClsInvDriverCost As New ClassInvDriverCost
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDriverCost.ConnectionOpen(StrConn)
        Dim ret As QueryInvDriverCosts = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvDriverCost"
        If ParaCostId & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(CostId = " & ParaCostId & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        End If
        Dim ds As DataSet
        With ClsInvDriverCost
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvDriverCosts

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvDriverCost( _
                              .Item("CostId") & "", _
                              .Item("CostDay") & "", _
                              .Item("CostHoliday") & "", _
                              .Item("OtDay") & "", _
                              .Item("OtHoliday") & "", _
                              .Item("Costnight") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvDriverCost.ConnectionClose(Conn)
        Return ret
    End Function
    Public Function Load(Optional ByVal ParaCostId As String = "") As QueryInvDriverCosts
        Dim ClsInvDriverCost As New ClassInvDriverCost
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvDriverCost.ConnectionOpen()
        Dim ret As QueryInvDriverCosts = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvDriverCost"
        If ParaCostId & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(CostId = " & ParaCostId & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        Else
            Return Nothing
            Exit Function
        End If
        Dim ds As DataSet
        With ClsInvDriverCost
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvDriverCosts

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvDriverCost( _
                              .Item("CostId") & "", _
                              .Item("CostDay") & "", _
                              .Item("CostHoliday") & "", _
                              .Item("OtDay") & "", _
                              .Item("OtHoliday") & "", _
                              .Item("Costnight") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvDriverCost.ConnectionClose(Conn)
        Return ret
    End Function
End Class